package com.jsgt.greekfire.ui.main

import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.AppConfig
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.ToastUtils
import com.fhx.core.utils.VibrateHelp
import com.jsgt.greekfire.App
import com.jsgt.greekfire.LOGIN_STATUS
import com.jsgt.greekfire.R
import com.jsgt.greekfire.common.widget.TabButtonContainer
import com.jsgt.greekfire.ui.dialog.DialogUtils
import com.jsgt.greekfire.ui.entrust.EntrustFragment
import com.jsgt.greekfire.ui.home.entrance.EntranceActivity
import com.jsgt.greekfire.ui.me.MeFragment
import com.jsgt.greekfire.ui.news.NewsFragment
import com.jsgt.greekfire.ui.wallet.WalletFragment
import com.jsgt.greekfire.viewmodel.UserViewModel
import kotlinx.android.synthetic.main.activity_main_atr.*

class MainAtrActivity : BaseActivity(), View.OnClickListener {
    private val userViewModel: UserViewModel by lazy {
        ViewModelProvider(App.context).get(UserViewModel::class.java)
    }
    val list: ArrayList<Class<*>> = ArrayList()

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        tabContainer.resetState()
        setLoginState(LOGIN_STATUS)
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_main_atr
    }

    override fun needFitsSystemWindows(): Boolean {
        return false
    }

    override fun needSetStatusBar(): Boolean {
        return false
    }

    var pattern = longArrayOf(50, 100, 150)
    override fun main() {
        setStatusBar(R.color.transparent, false, R.color.white)

        list.add(WalletFragment::class.java) //首页
        list.add(EntrustFragment::class.java) //委托
//        list.add(NewsFragment::class.java) //资讯
        list.add(MeFragment::class.java) //我的
        tabContainer.setUp(R.id.fl_fragment_container, supportFragmentManager, list)
        setLoginState(LOGIN_STATUS)
        tabContainer.resetState()
        tabContainer.loginClick = { EntranceActivity.start(this) }
        tabContainer.onTabSelectedListener =
            object : TabButtonContainer.OnTabSelectedListener {
                override fun onTabSelected(index: Int) {
                    // 当页面切换完成之后， 同时也要把 tab 设置到正确的位置
                    VibrateHelp.vComplicated(this@MainAtrActivity, pattern, -1)
                }
            }

        userViewModel.versionLiveData.observe(this, Observer {
            if (AppConfig.getVersion()!! < it?.version!!) {
                DialogUtils.showUploadDialog(this, it)
            }
        })
        userViewModel.getVersion()
    }

    override fun onClick(v: View?) {
        TODO("Not yet implemented")
    }

    /**
     * 设置是否已创建/导入钱包
     */
    private fun setLoginState(isLogin: Boolean) {
        tabContainer.setLoginState(isLogin)
    }

    private var mLastClickBackTime //上次点击back键的时间
            : Long = 0

    override fun onBackPressed() {
        val curTime = System.currentTimeMillis()
        if (curTime - mLastClickBackTime > 2000) {
            mLastClickBackTime = curTime
            ToastUtils.show(this, getString(R.string.try_again_exit))
            return
        }
        super.onBackPressed()
    }
}
